import java.util.ArrayList;

public class HtmlReportWriter extends ReportWriter{

    protected String getFilename(Team t, String title)
    {
        String fn = "";
        for (int i = 0; i < t.students.size(); i++)
        {
            Student s = t.students.get(i); 
            if (s.preferredFormat == Student.HTML)
            {
                fn = fn + s.getFirstName() + s.getLastName();
                if (i < t.students.size() - 1) fn = fn + "_";
            }
        }
        fn = fn + ".html";
        return fn;
    }

    protected void writeHeader(StringBuffer sb, Team t, String title)
    {
        sb.append("<html>\n");
        sb.append("\n");
        sb.append("<head>\n");
        sb.append("\t<title>" + title + "</title>\n");

        sb.append(header);
        sb.append("\t<h1>" + title + "</h1>\t\n");
        sb.append("\t\n");
        sb.append("\t<h3>");

        for (int i = 0; i < t.students.size(); i++)
        {
            Student s = t.students.get(i);
            //sb.append(s.getFirstName() + " " + s.getLastName());
            String name = s.getFirstName() + " " + s.getLastName();
            // Display the umlaute correct in HTML:
            name = name.replace("ö", "&ouml;");
            name = name.replace("ä", "&auml;");
            name = name.replace("ü", "&uuml;");
            name = name.replace("Ä", "&Auml;");
            name = name.replace("Ö", "&Ouml;");
            name = name.replace("Ü", "&Uuml;");
            name = name.replace("é", "&eacute;");
            name = name.replace("è", "&egrave;");
            sb.append(name);
            
            if (i < t.students.size() - 2) sb.append(", ");
            else if (i == t.students.size() - 2) sb.append(" und ");
        }
        
        sb.append("<h3>\n");
        sb.append("\t\n\n");
        sb.append("\t&nbsp;<br>&nbsp;\n\n");
    }

    protected void writeBreakdown(StringBuffer sb, Team t, String[] taskNames, double[] points, double[] maxPoints)
    {
        sb.append("\t<h2>&Uuml;bersicht</h2>\n");
        sb.append("\t<table>\n");
        sb.append("\t\t<tr>\n");

        for (int i = 0; i < taskNames.length; i++)
        {
            sb.append("\t\t\t<th>" + taskNames[i] + "</th>\n");
        }
        sb.append("\t\t\t<th class=\"sum\">Summe</th>\n");

        sb.append("\t\t</tr>\n");
        sb.append("\t\t\t");

        double total = 0.0, totMax = 0.0;
        for (int i = 0; i < points.length; i++)
        {
            sb.append("<td>" + points[i] + "/" + maxPoints[i] + "</td> ");
            total += points[i];
            totMax += maxPoints[i];
        }
        sb.append("\n");

        sb.append("\t\t\t<td class=\"total\">" + total + "/" + totMax + "</td>\n");
        sb.append("\t</table>\n\n");
        sb.append("\t&nbsp;<br>&nbsp;\n\n");
    }

    protected void writeIssues(StringBuffer sb, Team t, ArrayList<Issue> sortedIssues)
    {
        sb.append("\t<h2>Boni und Abz&uuml;ge</h2>\n");
        sb.append("\t<table>\n");
        sb.append("\t\t<tr>\n");
        sb.append("\t\t\t<th>Aufgabe</th> <th>Bemerkung</th> <th class=\"mod\">Auswirkung</th>\n");
        sb.append("\t\t</tr>\n");

        for (int i = 0; i < sortedIssues.size(); i++)
        {
            Issue s = sortedIssues.get(i);

            sb.append("\t\t<tr>\n");
            sb.append("\t\t\t<td class=\"index\">" + s.task.name + "</td>\n");

            String comment = s.comment;
            comment = comment.replace("\n","<br>");
            
            // Display the umlaute correct in HTML:
            comment = comment.replace("ö", "&ouml;");
            comment = comment.replace("ä", "&auml;");
            comment = comment.replace("ü", "&uuml;");
            comment = comment.replace("Ä", "&Auml;");
            comment = comment.replace("Ö", "&Ouml;");
            comment = comment.replace("Ü", "&Uuml;");
            comment = comment.replace("é", "&eacute;");
            comment = comment.replace("è", "&egrave;");
            
            sb.append("\t\t\t<td>" + comment + "</td>\n");

            if (s.value > 0.0)
                sb.append("\t\t\t<td class=\"good\">+" + s.value + "</td>\n");
            else if (s.value < 0.0)
                sb.append("\t\t\t<td class=\"bad\">" + s.value + "</td>\n");
            else sb.append("\t\t\t<td class=\"neutral\">-</td>\n");

            sb.append("\t\t</tr>\n");
        }

        sb.append("\t</table>\n\n");
        sb.append("\t&nbsp;<br>&nbsp;\n\n");

        sb.append("\t<p>\n");
        sb.append("\t\tMit freundlichen Gr&uuml;ssen,\n");
        sb.append("\t\t<a href=\"mailto:" + Settings.LOGIN + "\">" + Settings.NAME + "</a>\n");
        sb.append("\t</p>\n\n");

        sb.append("</body>\n</html>\n");
    }


    private static String header =
            "\t<style type=\"text/css\">\n" +
            "\t\tbody {margin-left: 6%; margin-right: 6%; margin-top: 3%; font-family: Georgia;}\t\n" +
            "\t\ttable {border-collapse: collapse;}\n" +
            "\t\ttd {\n" +
            "\t\t\tborder-left: 1px solid #505050;\n" +
            "\t\t\tborder-bottom: 1px solid #505050;\n" +
            "\t\t\tborder-top: 1px solid #505050;\t\t\t\n" +
            "\t\t\tpadding: 12px 24px 12px 24px;\n" +
            "\t\t\tbackground: #fff5b1;\n" +
            "\t\t}\n" +
            "\t\ttd.good{\n" +
            "\t\t\tcolor: #00a000;\n" +
            "\t\t\tfont: 100% Verdana;\n" +
            "\t\t\tfont-weight: bold;\n" +
            "\t\t\tborder-right: 1px solid #505050;\n" +
            "\t\t\ttext-align: center;\n" +
            "\t\t}\n" +
            "\t\ttd.bad{\n" +
            "\t\t\tcolor: #f00000;\n" +
            "\t\t\tfont: 100% Verdana;\n" +
            "\t\t\tfont-weight: bold;\n" +
            "\t\t\tborder-right: 1px solid #505050;\n" +
            "\t\t\ttext-align: center;\n" +
            "\t\t}\n" +
            "\t\ttd.neutral{\n" +
            "\t\t\tcolor: #000000;\n" +
            "\t\t\tfont: 100% Verdana;\n" +
            "\t\t\tfont-weight: bold;\n" +
            "\t\t\tborder-right: 1px solid #505050;\n" +
            "\t\t\ttext-align: center;\n" +
            "\t\t}\n" +
            "\t\ttd.total{\n" +
            "\t\t\tcolor: #000000;\n" +
            "\t\t\tborder-left: 1px solid #000000;\n" +
            "\t\t\tborder-right: 1px solid #505050;\n" +
            "\t\t\tfont: 100% Verdana;\n" +
            "\t\t\tfont-weight: bold;\n" +
            "\t\t}\n" +
            "\t\ttd.index{\n" +
            "\t\t\tcolor: #000000;\n" +
            "\t\t\tfont: 100% Verdana;\n" +
            "\t\t\tfont-weight: bold;\n" +
            "\t\t}\n" +
            "\t\t\n" +
            "\t\tth {\n" +
            "\t\t\tborder-left: 1px solid #c8c850;\n" +
            "\t\t\tborder-bottom: 1px solid #b6a650;\n" +
            "\t\t\tborder-top: 1px solid #ffffc0;\n" +
            "\t\t\tpadding: 18px 24px 18px 24px;\n" +
            "\t\t\tbackground: #ffe76c;\n" +
            "\t\t}\n" +
            "\t\tth.sum {\n" +
            "\t\t\tborder-left: 1px solid #000000;\n" +
            "\t\t\tborder-right: 1px solid #505050;\n" +
            "\t\t}\n" +
            "\t\tth.mod {\t\t\t\n" +
            "\t\t\tborder-right: 1px solid #505050;\n" +
            "\t\t}\n" +
            "\t\t\n" +
            "\t\tA:link, active {font: 80% Verdana; color: #404e96;}\n" +
            "\t\tA:visited {font: 80% Verdana; color: #242c54;}\n" +
            "\t\tA:hover {font: 80% Verdana; color: #6c85ff;}\n" +
            "\t\t.logo\n" +
            "\t\t{\n" +
            "\t\t\tfloat: right;\n" +
            "\t\t\tmargin: 0px 0px 0px 0px;\n" +
            "\t\t\tborder: 0px solid #666;\n" +
            "\t\t\tpadding: 2px;\n" +
            "\t\t\tposition: absolute;\n" +
            "\t\t\tright: 12%; top: -1%;\n" +
            "\t\t}\n" +
            "\t</style>\n" +
            "</head>\n" +
            "\n" +
            "<body bgcolor=\"#f5e4b0\">\n" +
            "\n" +
            "\t<img class=\"logo\" src=\"http://informatik.unibas.ch/lehre/hs08/cs201/_Downloads/logo_feedback.png\" alt=\"\" width=\"154\" height=\"181\">\n" +
            "\t\n";
}
